<template>
	<view class="fxDetails">
		<view class="details-box" v-if="fxDetails">
			<view class="fx-title">
				{{fxDetails.title}}
			</view>
			<view class="good-location">
				位置：{{fxDetails.location}}
			</view>
			<view class="fx-content">
				描述信息：{{fxDetails.content}}
			</view>
			<view class="good-money">
				价格：￥<view class="money">
					{{fxDetails.money}}
				</view>
			</view>
			<view class="fx-images">
				<img v-for="(item,index) in ImageList" :src="item" :key="index" alt="" srcset="">
			</view>
			<view class="fx-time">
				上架时间：{{fxDetails.createTime}}
			</view>
			<u-button type="error" text="立即购买" @click="buyGoods"></u-button>
		</view>
		<u-popup :show="show" mode="bottom" @close="close">
			<view class="popup_content">
				<view class="yhq-one" v-for="(item,index) in userYhq" :key="index" @click="yhqClick(item)">
					<view class="title">
						{{item.conDetails.title}}
					</view>
					<view class="content">
						备注：{{item.conDetails.content}}
					</view>
					<view class="money">
						￥{{item.conDetails.Money}}
					</view>
				</view>
			</view>
		</u-popup>
		<u-modal :show="showModal" title="购买" :showCancelButton="true" @cancel="showModal = false" :content="content" @confirm="confirm" ref="uModal" :asyncClose="true"></u-modal>
	</view>
</template>

<script>
	import http from "../../static/http.js"
	export default {
		name: "",
		data() {
			return {
				rowDetailsId: "",
				fxDetails: "",
				ImageList: [],
				show: false,
				userYhq:[],
				showModal:false,
				content:"",
				couponDetails:{}
			}
		},
		onLoad(params) {
			this.rowDetailsId = params.id
		},
		created() {
			this.getDetails()
		},
		methods: {
			buyGoods() {
				http.httpRequest("/userCoupon/getyhq", "", "GET").then(res => {
					if(res.data.length>0){
						this.userYhq = res.data
						this.show = true
					}else{
						uni.showToast({
							title:"暂无优惠券",
							icon:"none"
						})
						this.content = `使用金额为${this.fxDetails.money}的优惠券购买${this.fxDetails.title}`
						this.showModal = true
						this.couponDetails = {}
					}
				})
			},
			yhqClick(item){
				if(item.conDetails.Money > this.fxDetails.money){
					uni.showToast({
						title:"无法使用这张优惠券！",
						icon:'none'
					})
					return
				}
				this.close()
				this.content = `使用金额为${item.conDetails.Money}的优惠券购买${this.fxDetails.title}`
				this.showModal = true
				this.couponDetails = item
			},
			confirm(){
				let data = {
					goods:JSON.stringify(this.fxDetails),
					coupon:JSON.stringify(this.couponDetails),
					money: this.fxDetails.money - (Object.values(this.couponDetails).length>0?this.couponDetails.conDetails.Money:0)
					
				}
				console.log(data);
				http.httpRequest("/order/add",data,"POST").then(res=>{
					if(Object.values(this.couponDetails).length>0){
						http.httpRequest("/userCoupon/update",{id:this.couponDetails.id},"POST").then(resp=>{
							this.showModal = false
							uni.showToast({
								title:"购买成功！",
								icon:"none"
							})
						})
					}else{
						this.showModal = false
						uni.showToast({
							title:"购买成功！",
							icon:"none"
						})
					}
				})
			},
			getDetails() {
				http.httpRequest("/article/getDetail?id=" + this.rowDetailsId, ``, "GET").then(res => {
					res.data.imageList.forEach(val => {
						this.ImageList.push(http.BASE_URL + val.url)
					})
					this.fxDetails = res.data
				})
			},
			close() {
				this.show = false
			}
		}
	}
</script>

<style lang="less">
	.fxDetails {
		width: 100%;
		height: 100%;
		background-color: #f0f0f5;

		.details-box {
			width: 98%;
			height: 100%;
			margin: 0 auto;
			background-color: #fff;

			.fx-title {
				width: 100%;
				font-size: 35rpx;
				font-weight: 600;
				padding-top: 10rpx;
			}

			.good-location {
				margin-top: 10rpx;
				font-size: 30rpx;
			}

			.good-money {
				.money {
					display: inline-block;
					font-size: 40rpx;
					color: #dd6161;
				}
			}

			.fx-content {
				width: 100%;
				font-size: 28rpx;
				padding-top: 10rpx;
			}

			.fx-images {
				width: 100%;
				height: auto;

				img {
					width: 100%;
					height: 300rpx;
					object-fit: contain;
					margin-top: 20rpx;
				}
			}

			.fx-time {
				padding-bottom: 10rpx;
			}
		}

		.popup_content {
			width: 100%;
			max-height: 500rpx;

			.yhq-one {
				width: 95%;
				min-height: 100rpx;
				margin: 10rpx auto;
				background-color: #409EFF;
				border-radius: 10rpx;
				-webkit-mask: radial-gradient(circle at 20px, #0000 20px, red 0) -20px;

				.title {
					padding: 10rpx;
					font-size: 40rpx;
				}

				.content {
					padding: 10rpx;
					font-size: 30rpx;
					color: #fff;
				}

				.money {
					padding: 10rpx;
					color: #dd6161;
					font-size: 50rpx;
					margin-right: 25rpx;
					text-align: right;
				}
			}
		}
	}
</style>
